//
//  URLoadViewContoller.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/10/20.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class URLoadViewContoller: NSViewController {
    
    override func loadView() {
        let view = NSView.init(frame: NSRect.init(origin: CGPoint.zero, size: CGSize.init(width: 420, height: 130)))
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(230).cgColor
        self.view = view
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.refreshrtingWindow()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    func loadData()  {
        self.indicator.startAnimation(nil)
        
        Mmanager.checkComp(suess: { (isUpdate, result) in
            self.indicator.stopAnimation(nil)
            self.view.window?.close()
            if isUpdate {
                if let data = result["data"] as? [String:Any] {
                    if let url = data["url"] as? String ,let version = data["version"] as? String {
                        self.runModal(newVersion: version,urlString: url)
                    }
                }
            }else {
                let controller = VersionController.init()
                controller.isCheck = true
                NSWindow.init(contentViewController: controller).lorderFront(nil)
            }
        }) {
            self.indicator.stopAnimation(nil)
            self.view.window?.close()
        }
    }
    
    func runModal(newVersion:String,urlString:String?) {
        let alert = NSAlert.init()
        alert.messageText = "发现新版本的 \(newVersion)，当前版本 \(AppInfo.majorVersion())，是否立即下载更新？"
        alert.addButton(withTitle: "下载更新")
        alert.addButton(withTitle: "取消")
        let respose = alert.runModal()
        if respose == NSApplication.ModalResponse.alertFirstButtonReturn {
            let window = NSWindow.init(contentViewController: URAlertViewConroller.init(urlString: nil))
            window.lorderFront(nil)
        }
    }
    
    func refreshrtingWindow() {
        self.view.window?.standardWindowButton(NSWindow.ButtonType.closeButton)?.isHidden = true
        self.view.window?.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)?.isHidden = true
        self.view.window?.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = true
        self.view.window?.styleMask = [.titled]
        self.title = "检查中"
    }
    
    lazy var indicator: NSProgressIndicator = {
        let view = NSProgressIndicator.init()
        view.style = .spinning
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.clear.cgColor
        view.controlSize = .regular
        view.sizeToFit()
        return view
    }()
    
    
    func setupUI() {
        self.view.addSubview(indicator)
        indicator.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-12)
        }
        
        self.view.addSubview(ktitle)
        ktitle.snp.makeConstraints { (make) in
            make.height.equalTo(18)
            make.centerX.equalToSuperview()
            make.top.equalTo(indicator.snp.bottom).offset(5)
        }
        
    }
    
    lazy var rightView: NSView = {
        let view = NSView.init()
        return view
    }()
    
    lazy var iconImage: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.image(name:"ic_logo_blue") ?? NSImage.init())
        return imageV
    }()
    
    lazy var ktitle: NSLabel = {
        let lab = NSLabel.label(title: "正在下载更新", textColor: color_gray(120), font: NSFont.systemFont(ofSize: 12))
        lab.sizeToFit()
        lab.alignment = .center
        return lab
    }()
}
